﻿using System;
using System.Collections.Generic;
using System.Linq;
using DAL;

namespace Langben.BLL
{
    public class SysDepartmentBLL :  IDisposable
    {
        /// <summary>
        /// 私有的数据访问上下文
        /// </summary>
        protected SysEntities db;
        /// <summary>
        /// 部门的数据库访问对象
        /// </summary>
        SysDepartmentRepository repository = new SysDepartmentRepository();
        /// <summary>
        /// 构造函数，默认加载数据访问上下文
        /// </summary>
        public SysDepartmentBLL()
        {
            db = new SysEntities();
        }
        /// <summary>
        /// 已有数据访问上下文的方法中调用
        /// </summary>
        /// <param name="entities">数据访问上下文</param>
        public SysDepartmentBLL(SysEntities entities)
        {
            db = entities;
        }

        public List<SysDepartment> GetAll()
        {           
            return repository.GetAll(db).ToList();          
        }   
        
        /// <summary>
        /// 获取自连接树形列表数据
        /// </summary>
        /// <returns>自定义的树形结构</returns>
                   
        public IQueryable<SysDepartment> GetAllMetadata(string id)
        { 
            if (id == null)
            {
                return db.SysDepartment.Where(w => w.ParentId == null).AsQueryable();
            }
            else
            {
                return db.SysDepartment.Where(w => w.ParentId == id).AsQueryable();
            }
        }

            
        /// <summary>
        /// 根据主键获取一个部门
        /// </summary>
        /// <param name="id">部门的主键</param>
        /// <returns>一个部门</returns>
        public SysDepartment GetById(string id)
        {           
            return repository.GetById(db, id);           
        }


        public void Dispose()
        {
           
        }
    }
}

